package com.uw.gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;


@Slf4j
public class UwGatewayFilter implements WebFilter {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        log.info("===>>> UW Gateway web filter ...");
        if (exchange.getRequest().getQueryParams().getFirst("mock") == null) {
            return chain.filter(exchange);
        }

        String mockResult = """
                {
                    "result": "mock"
                }
                """;
        return exchange.getResponse().writeWith(Mono.just(exchange.getResponse().bufferFactory().wrap(mockResult.getBytes())));
    }
}
